package context.filter;

import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "CodeFilter")
public class CodeFilter implements Filter {
    public void destroy() {
    }



    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("接收到了请求");
        req.setCharacterEncoding("UTF-8");
        HttpServletResponse response = (HttpServletResponse) resp;
//        HttpServletRequest request = (HttpServletRequest) req;
//        String servletUrl = request.getServletPath().substring(request.getServletPath().lastIndexOf("/"), request.getServletPath().length());
//
//        request.getRequestURI();
//        String username = (String) httpSession.getAttribute("username");
//        if (username == null || username.trim().isEmpty()) {  //没有登录
//            //用户如果不是访问的首页、登录、注册页  那就拦截请求
//            if (!servletUrl.equals("login") || !servletUrl.equals("regist") || !servletUrl.equals("home") || !servletUrl.equals("home")) {
//                HttpServletRequest response = (HttpServletRequest) resp;
//
//                request.getRequestDispatcher("/login").forward(req, resp);   //把请求转发到登录页
//            }
//        }


        chain.doFilter(req, resp);  //传递下去
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
